<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>

    //手写map
    const arr = [1, 2, 3, 4, 5]

    // map是把数组的每一项执行回调函数，并返回给新数组
    let arr1 = arr.map(function (item) {
      return item + 1
    })
    // console.log(arr1);

    Array.prototype.myMap = function (fn) {
      let k = []
      for(let i = 0;i < this.length;i++) {
       k.push(fn(this[i]))
      }
      // return k
      // console.log(k);
    }

    arr.myMap(function(item){
      return item + 1
    })


    //手写filter
    // filter是筛选出符合回调函数内条件的元素，并返回给新数组
    let arr2 = arr.filter(function(item){
      return item >3
    })
    // console.log(arr2);

    Array.prototype.myFilter = function (fn) {
      let p = []
      for(let i =0; i < this.length;i++) {
      //  p.push(fn(this[i]))
       if(fn(this[i]) === true) {
          p.push(this[i])
       }
      }

      // return p
      console.log(p);
    }

    arr.myFilter(function (item) {
      return item > 2
    })

  </script>
</body>

</html>